package com.golf.comms.server;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import javax.management.MBeanServer;

import org.jboss.remoting.InvocationRequest;
import org.jboss.remoting.ServerInvocationHandler;
import org.jboss.remoting.ServerInvoker;
import org.jboss.remoting.callback.InvokerCallbackHandler;

import com.golf.server.selection.LobbyManager;
import com.golf.server.selection.User;

/**
 * 
 * 
 * @author gfloodgate
 */
public class LobbyInvocationHandler implements ServerInvocationHandler
{
	
	/** The list of registered callback handlers. */
	private List<InvokerCallbackHandler> callbacks = new CopyOnWriteArrayList<InvokerCallbackHandler>();
	
	
	/**
	 * {@inheritDoc}
	 */
	public Object invoke(final InvocationRequest request) throws Throwable
	{
		final LobbyInvocationCommand type = (LobbyInvocationCommand)request.getParameter();
		switch (type)
		{
			case LIST_ROOMS:
			{
				final LobbyManager manager = LobbyManager.getInstance();
				return manager.listRooms();
			}
			case ADD_ROOM:
			{
				final LobbyManager manager = LobbyManager.getInstance();
				return manager.createRoom("jimroom ", new User("bob"));
			}
		}
		
		
		return null;
	}

	 
	/**
	 * {@inheritDoc}
	 */
	public void addListener(final InvokerCallbackHandler handler)
	{
		callbacks.add(handler);
	}

	
	/**
	 * {@inheritDoc}
	 */
	public void removeListener(final InvokerCallbackHandler handler)
	{
		callbacks.remove(handler);
	}

	
	/**
	 * {@inheritDoc}
	 */
	public void setInvoker(final ServerInvoker invoker)
	{
		// Do nothing.
	}

	
	/**
	 * {@inheritDoc}
	 */
	public void setMBeanServer(final MBeanServer server)
	{
		// Do nothing.
	}

}
